using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using Dragon.Prototype1.Service;

namespace Dragon.Prototype1.Web.Controllers
{
    public class DepartmentController : Controller
    {
        //
        // GET: /Department/

        public ActionResult Index()
        {
            var data=DepartmentService.GetRootDepartments();
            ViewData["needIns"] = true;
            return View(data);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateDepartment(string departname,int parentid)
        {
            int newid=DepartmentService.CreateDepartment(departname, parentid);
            return Json(newid);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RenameDepartment(int id, string departname)
        {
            return Json(DepartmentService.RenameDepartment(id, departname));
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteDepartment(int id)
        {
            return Json(DepartmentService.DeleteDepartment(id));
        }

        public ActionResult TransferStaffBetweenDepartment()
        {
            var data = DepartmentService.GetRootDepartments();
            ViewData["needIns"] = false;
            return View(data);
        }
        //[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SetUnassignedUsers(Guid[] userids)
        {
            var result = DepartmentService.SetUnassignedUsers(userids);
            return Json(result);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UsersInDepartment(int id)
        {
            Dictionary<Guid,string> userlist  = DepartmentService.GetUsersInDepartment(id,null,null).ToDictionary(k=>k.UserId,k=>k.UserName);
            return View(userlist);
        }

        public ActionResult UsersInDepartmentDetail(int departid,string sidx,string sord,int page,int rows)
        {
            if (departid <= 0)
                return Json(false);
            var userdata= DepartmentService.GetUsersInDepartment(departid,sidx,sord);
            var jsondata = new
                {
                    page=1,
                    records=userdata.Count(),
                    rows=(from row in userdata
                       select new {
                           id=row.UserId.ToString(),
                           cell=new string[]{
                               row.UserId.ToString(),
                               row.UserName, 
                               row.Membership.Email, 
                               row.Membership.CreateDate.ToString("yyyy-MM-dd"),
                               row.Membership.LastLoginDate.ToString("yyyy-MM-dd")
                           }
                       }).ToArray()

                };
            return Json(jsondata);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AssignUsersToDepartment(Guid[] subpanel_id, int departid)
        {
            var userdata = DepartmentService.AssignUsersToDepartment(subpanel_id, departid);

            var jsondata = (from row in userdata
                            select new
                            {
                                id = row.UserId.ToString(),
                                cell = new
                                {
                                    id = row.UserId.ToString(),
                                    username = row.UserName,
                                    email = row.Email,
                                    createdate = row.CreateDate.ToString("yyyy-MM-dd"),
                                    lastlogindate = row.LastLoginDate.ToString("yyyy-MM-dd")
                                }
                            }).ToArray();

            return Json(jsondata);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MoveUsersBetweenDepartments(Guid[] userids, int nextDepartmentId, int prevDepartmentId)
        {
            return Json(DepartmentService.MoveUsersBetweenDepartments(userids, nextDepartmentId, prevDepartmentId));
        }
    }
}
